<template>
  <div class="login-page">
    <canvas id="cs-bg" />
    <div class="login-box">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        label-width="80px"
        class="login-form"
        label-position="left"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" />
        </el-form-item>
        <el-form-item class="btn-box">
          <el-button
            plain
            class="fl"
            @click="resetForm('loginForm')"
          >重置</el-button>
          <el-button
            plain
            class="fr"
            @click="submitForm('loginForm')"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import canvasBg from '../utils/cs-bg'
import { login } from '../api/login'
import '../mock/index.js'
export default {
  data() {
    return {
      loginForm: {
        username: 'editor',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    canvasBg()
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate(async valid => {
        if (valid) {
          console.log(login)
          const res = await login(_this.loginForm)
          console.log('res:', res)
          const { message, code } = res.data
          if (code === 200) {
            localStorage.setItem('token', 'abc')
            localStorage.setItem('role', res.data.role)
            _this.$router.push('/')
            _this.$store.dispatch('user/role', res.data.role)
            // _this.$store.dispatch('permission/getRoute', res.data.role)
          } else if (code === 401) {
            _this.$message.error('用户信息已过期')
            localStorage.removeItem('token')
            _this.$router.push('/login')
          } else if (code === 402) {
            _this.$message(message)
          }
        } else {
          this.$message({
            message: '用户名或密码有误！',
            type: 'error'
          })
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="less">
#cs-bg {
  position: absolute;
  left: 0;
  top: 0;
  z-index: -99;
  background: #000;
}

.login-box {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -80%) rotate(-0deg);
  width: 350px;
  padding: 40px;
  padding-top: 60px;
  //   border-radius: 20px;
  background-color: rgba(255, 255, 255, 0.2);
}

/deep/ .el-input__inner {
  background-color: rgba(255, 255, 255, 0.3);
  color: #fff;
}

/deep/ .el-form-item__label {
  color: #fff;
}

.btn-box /deep/ .el-form-item__content {
  display: flex;
  justify-content: space-between;
}
</style>
